package com.edu.demo.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.edu.demo.entity.Student;
import com.edu.demo.entity.Teacher;
import com.edu.demo.mapper.StudentMapper;
import com.edu.demo.pojo.AjaxResult;
import com.edu.demo.service.IStudentService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.logging.log4j.util.Strings;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author jixuewei
 * @since 2023-01-12
 */
@Service
public class StudentServiceImpl extends ServiceImpl<StudentMapper, Student> implements IStudentService {

    @Autowired
    private StudentMapper studentMapper;

    @Autowired
    private BCryptPasswordEncoder bCryptPasswordEncoder;

    public List<Student> studentList(int index, int size, AjaxResult result){
        IPage<Student> page = new Page(index,size);
        QueryWrapper wrapper = new QueryWrapper();

        IPage<Student> iPage = studentMapper.selectPage(page, wrapper);
        List<Student> records = iPage.getRecords();
        records.forEach(student -> student.setPassword(""));
        result.put("total",iPage.getTotal());
        return records;

    }

    public int updateStudent(Student student){
//        if(Objects.isNull(student.getPassword())){
//        }else {
//            //student.setPassword(bCryptPasswordEncoder.encode(student.getPassword()));
//        }
       return studentMapper.updateById(student);
    }

    public int insertStudent(Student student){

       return studentMapper.insert(student);
    }

    /**
     * 按条件查询学生
     *
     * @param student
     * @return
     */
    @Override
    public List<Student> findStudents(Student student,int index,int size,AjaxResult ajaxResult) {

        IPage<Student> page = new Page<>(index,size);
        QueryWrapper<Student> queryWrapper = new QueryWrapper<>();
        if(Strings.isNotEmpty(student.getName()))
            queryWrapper.likeLeft("name",student.getName());
        if(Strings.isNotEmpty(student.getClazz()))
            queryWrapper.eq("class",student.getClazz());
        if(Strings.isNotEmpty(student.getPhone()))
            queryWrapper.likeLeft("phone",student.getPhone());
        if(!Objects.isNull(student.getDeptId()))
            queryWrapper.eq("dept_id",student.getDeptId());
        if(Strings.isNotEmpty(student.getDeptName()))
            queryWrapper.likeLeft("dept_name",student.getDeptName());
        if(Strings.isNotEmpty(student.getCode()))
            queryWrapper.eq("code",student.getCode());
        if(Strings.isNotEmpty(student.getMajor()))
            queryWrapper.eq("major",student.getMajor());
        if(!Objects.isNull(student.getStatus()))
            queryWrapper.eq("status",student.getStatus());
        IPage<Student> page1 = studentMapper.selectPage(page, queryWrapper);
        List<Student> students = page1.getRecords();
        ajaxResult.put("total",page1.getTotal());
        students.forEach(student1 -> student1.setPassword(null));
        return students;
    }
}
